<template>
  <div class="page-user-label">
    <div class="title">
      {{ $t("user.allLabels") }}
    </div>
    <div class="content">
      <div class="tabs">
        <el-tabs>
          <el-tab-pane :label="$t('user.conditionLabel')">
            <div class="bg-white add-label btn-bar">
              <el-button
                type="primary"
                @click="showEditFrame('',1)"
              >
                {{ $t('user.addLabel') }}
              </el-button>
              <el-button
                @click="onRefreshLabel()"
              >
                {{ $t('user.updateLabelData') }}
              </el-button>
              <div class="font-line">
                {{ $t('user.updateTime') }}：{{ (customLabelList[customLabelList.length-1]?.statisticUpdateTime) ? customLabelList[customLabelList.length-1].statisticUpdateTime : '暂无更新' }}
              </div>
            </div>
            <!-- 不同标签内容 -->
            <div class="bg-white label-content-wrapper">
              <!-- 自定义标签 -->
              <div class="custom-label label-box">
                <div class="label-type-wrapper">
                  <span class="label-type">{{
                    $t("user.customConditionLabel")
                  }}</span>
                  <span class="label-tips">{{
                    $t("user.customConditionLabelTips")
                  }}</span>
                </div>
                <div
                  v-if="customLabelList.length"
                  class="label-content"
                >
                  <div
                    v-for="(item, index) in customLabelList"
                    :key="index"
                    class="label-item"
                  >
                    <div class="label-name">
                      {{ item.tagName }}
                    </div>
                    <div class="font-line">
                      {{ $t("user.peopleNum") }}：{{ item.userNum }}
                    </div>
                    <div class="font-line">
                      {{ $t("user.creationTime") }}：{{ item.createTime }}
                    </div>
                    <div class="op-btn-wrapper">
                      <span @click="showEditFrame(item.userTagId, 1)">{{
                        $t("user.editBtn")
                      }}</span>
                      <span @click="deleteLabel(item.userTagId)">{{
                        $t("user.deleteBtn")
                      }}</span>
                    </div>
                  </div>
                </div>
                <div
                  v-else
                  class="label-content"
                >
                  <div class="data-empty">
                    {{ $t('order.noData') }}
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane :label="$t('user.manualLabel')">
            <div class="bg-white add-label">
              <div class="label-wapper">
                <el-button
                  type="primary"
                  @click="showEditFrame('', 0)"
                >
                  {{ $t("user.addLabel") }}
                </el-button>
              </div>
            </div>
            <!-- 标签内容 -->
            <div class="bg-white label-content-wrapper">
              <!-- 手动标签 -->
              <div class="custom-label label-box">
                <div class="label-type-wrapper">
                  <span class="label-type">{{ $t("user.manualLabel") }}</span>
                </div>
                <div
                  v-if="manualLabelList.length"
                  class="label-content"
                >
                  <div
                    v-for="(item, index) in manualLabelList"
                    :key="index"
                    class="label-item"
                  >
                    <div class="label-name">
                      {{ item.tagName }}
                    </div>
                    <div class="font-line">
                      {{ $t("user.peopleNum") }}：{{ item.userNum }}
                    </div>
                    <div class="font-line">
                      {{ $t("user.creationTime") }}：{{ item.createTime }}
                    </div>
                    <div class="op-btn-wrapper">
                      <span @click="showEditFrame(item.userTagId, 0)">{{
                        $t("user.editBtn")
                      }}</span>
                      <span @click="deleteLabel(item.userTagId)">{{
                        $t("user.deleteBtn")
                      }}</span>
                    </div>
                  </div>
                </div>
                <div
                  v-else
                  class="label-content"
                >
                  <div class="data-empty">
                    {{ $t('order.noData') }}
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <!-- 新增/编辑标签 -->
    <UserLabelEdit
      v-if="isShowEditFrame"
      ref="addOrEditRef"
      @refresh-label-list="addOrEditSucc"
    />
  </div>
</template>

<script setup>
import UserLabelEdit from './add-or-update.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

onMounted(() => {
  getCustomLabelList()
})

// 新增弹窗
const addOrEditRef = ref(null)
const isShowEditFrame = ref(false)
const showEditFrame = (userTagId, tagType) => {
  isShowEditFrame.value = true
  nextTick(() => {
    addOrEditRef.value?.init(userTagId, tagType)
  })
}
// 获取自定义标签列表
const customLabelList = ref([])
const manualLabelList = ref([])
const getCustomLabelList = () => {
  http({
    url: http.adornUrl('/user/userTag/getTagList'),
    method: 'get',
    params: http.adornParams()
  }).then(({ data }) => {
    const newCustomLabelList = []
    const newManualLabelList = []
    for (let i = 0; i < data.length; i++) {
      if (data[i].tagType === 1) {
        newCustomLabelList.push(data[i])
      } else if (data[i].tagType === 0) {
        newManualLabelList.push(data[i])
      }
    }
    customLabelList.value = newCustomLabelList
    manualLabelList.value = newManualLabelList
  })
}

const onRefreshLabel = () => {
  http({
    url: http.adornUrl('/user/userTag/batchRefresh'),
    method: 'GET',
    data: http.adornData({})
  }).then(() => {
    getCustomLabelList()
    ElMessage.success($t('user.updateSucceeded'))
  })
}

const deleteLabel = (userTagId) => {
  ElMessageBox.confirm($t('platform.isDeleOper'), $t('text.tips'), {
    confirmButtonText: $t('homes.yes'),
    cancelButtonText: $t('homes.no'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/user/userTag/' + userTagId),
      method: 'DELETE',
      data: http.adornData({})
    }).then(() => {
      getCustomLabelList()
      ElMessage.success($t('user.deletionSucceeded'))
    })
  }).catch(() => {
  })
}
const addOrEditSucc = (data) => {
  isShowEditFrame.value = false
  if (data) {
    getCustomLabelList()
  }
}

</script>

<style lang="scss" scoped>
.page-user-label {
  .title {
    font-size: 16px;
    font-weight: 700;
    padding: 10px 10px 10px 0;
    margin-bottom: 15px;
  }

  .content {
    min-height: 300px;
    padding: 10px;
    background: #fff;
    border-radius: 6px;
    .label-content-wrapper {
      background: #f2f2f6;
    }
    .label-content {
      .label-item {
        background: #fff;
      }
    }
  }

  /* 标签选择 */
  .content .tabs {
    margin: 10px 0 12px;
  }

  .content .tabs :deep(.el-tabs__header) {
    margin: 0;
  }

  /* 新增标签 */
  .content .add-label {
    display: flex;
    padding: 10px;
    border-radius: 6px;
    margin-bottom: 10px;
  }

  /* 不同类型标签的内容 */
  .content .label-content-wrapper {
    padding: 10px 10px 10px 20px;
    border-radius: 6px;
  }

  .content .label-content-wrapper .label-box .label-type-wrapper {
    margin: 10px 0;
  }

  .content .label-content-wrapper .label-box .label-type-wrapper .label-type {
    font-size: 16px;
    font-weight: bold;
  }

  .content .label-content-wrapper .label-box .label-type-wrapper .label-tips {
    font-size: 12px;
    color: #9797a1;
    padding-left: 10px;
  }

  .content .label-content-wrapper .label-box .label-content {
    display: flex;
    flex-wrap: wrap;
  }

  .content .label-content-wrapper .label-box .label-content .label-item {
    position: relative;
    box-sizing: content-box;
    width: 198px;
    height: 100px;
    font-size: 12px;
    padding: 10px;
    border: 1px solid #e3e2e5;
    border-radius: 5px;
    margin: 0 5px 10px 0;
  }

  .content .label-content-wrapper .label-box .label-content .label-item .font-line {
    margin-bottom: 10px;
  }

  .content .label-content-wrapper .label-box .label-content .label-item .label-name {
    height: 30px;
    font-weight: 700;
    padding: 0 0 10px;
  }
  .content .label-content-wrapper .label-box .label-content .label-item .op-btn-wrapper {
    position: absolute;
    bottom: 10px;
    right: 10px;
    color: #2589ff;
  }
  .content .label-content-wrapper .label-box .label-content .label-item .op-btn-wrapper span {
    cursor: pointer;
    margin-left: 10px;
  }

  // 弹框
  :deep(.el-dialog) {
    width: 1000px;
  }

  .data-empty {
    width: 100%;
    line-height: 24px;
    color: #999;
    text-align: center;
  }

  .btn-bar {
    display: flex;
    align-items: end;

    .font-line {
      margin-left: 10px;
      padding-bottom: 5px;
    }
  }

}
</style>
